****************************************************************************************************************
**																											  **
** This do file is part of the replication material for the following article: 								  **
**  "Is There an Ideological Asymmetry in the Incumbency Effect? Evidence from U.S. Congressional Elections." **
** 		Authors: Morisi, Davide, Jost, John, Panagopoulos, Costas, and Jussi Valtonen		    			  **
** 		Journal: Social Psychological and Personality Science																		  **
**																											  **
** This file replicates the following step of the analysis: 												  **
**																											  **
**		RECODING CCES 2012 ORIGINAL DATASET			   													  	  **
**																											  **
****************************************************************************************************************

*Set working directory
cd "C:\Users\Davide Morisi\Dropbox\datasets\CCES"

*Open original dataset
*Ansolabehere, Stephen; Schaffner, Brian, 2013, "CCES Common Content, 2012", https://doi.org/10.7910/DVN/HQEVPK, Harvard Dataverse, V9, UNF:5:Eg5SQysFZaPiXc8tEbmmRA== [fileUNF] 
*Downloaded on January 16, 2020 from https://dataverse.harvard.edu/dataset.xhtml?persistentId=hdl:1902.1/21447
use "2012\CCES12_Common_VV.dta", clear

***
*covariates

*gender
fre gender // 2=female
*age
sum birthyr
*education
fre educ 
*race
fre race
recode race 4/8=99, gen(racer) // 99=other
*religion
fre religpew
recode religpew 3/8=3 12=3 9=4 10=5 11=6, gen(religpewr)
fre religpewr
*economy
fre CC302
rename CC302 economy
*state
fre inputstate
rename inputstate state
clonevar state12 = state
*weight
sum weight_vv weight_vv_post V103
rename weight_vv_post weight1


*********************
*partisanship / ideology

*party of respondent - profile
*3 categories
*1 democrat, 2 republican, 3 independent, 4 other, 5 dk
fre pid3
*7 categories
*1 strong dem, 2 not strong dem, 3 lean dem, 4 indep, 5 lean rep, 6 not strong rep, 7 strong rep, 8 dk 
fre pid7
ta pid7 pid3 // leaning partisans coded as independent/other/dk

*party id post
*3 categories (note: cat 5 dk missing)
fre CC421a // pid3
rename CC421a pid3post
*5 categories
fre CC421b // leaners
rename CC421b leaners_prof_12
ta pid3post if leaners_prof_12!=. // asked only for independents/others, and 2 republicans
fre pid3post leaners_prof_12
gen pid5post = 1 if pid3post==1
replace pid5post = 2 if pid3post>2 & leaners_prof_12==1
replace pid5post = 3 if pid3post==3 & leaners_prof_12==3
replace pid5post = 4 if pid3post>2 & leaners_prof_12==2
replace pid5post = 5 if pid3post==2
*1 dem, 2 lean dem, 3 indep, 4 lean rep, 5 rep
fre pid5post


*ideology profile
fre ideo5

*ideology from PRE-ELECTION SURVEY
*1 very lib, 2 lib, 3 somewhat lib, 4 mod, 5 somewhat con, 6 con, 7 very con, 8dk
fre CC334A
rename CC334A ideo7

*****
*vote post
*turnout
fre CC401
rename CC401 turnout12
clonevar turnout = turnout12
*senate
fre CC410b
rename CC410b votesen12
clonevar vote_sen = votesen12

*house
fre CC412
rename CC412 votehou12
clonevar vote_hou = votehou12


*****************
*contextual var - SENATE

*incumbent candidates
des SenCand1Incumbent_post SenCand2Incumbent_post
fre SenCand1Incumbent_post SenCand2Incumbent_post
encode(SenCand1Incumbent_post), gen(SenCand1Incumbent_postr)
encode(SenCand2Incumbent_post), gen(SenCand2Incumbent_postr)
fre SenCand1Incumbent_postr SenCand2Incumbent_postr
recode SenCand1Incumbent_postr 1=0 2=1
recode SenCand2Incumbent_postr 1=0 2=1
label de incumb 0"not incumbent" 1"incumbent", modify
label values SenCand1Incumbent_postr incumb
label values SenCand2Incumbent_postr incumb
fre SenCand1Incumbent_postr SenCand2Incumbent_postr
ta SenCand1Incumbent_postr SenCand2Incumbent_postr

*party of candidate
des SenCand1Party_post SenCand2Party_post
fre SenCand1Party_post SenCand2Party_post
encode SenCand1Party_post, gen(SenCand1Party_postr)
encode SenCand2Party_post, gen(SenCand2Party_postr)
fre SenCand1Party_postr SenCand2Party_postr
recode SenCand1Party_postr 1=1 2=3
recode SenCand2Party_postr 1=2
label de partycand 1"dem. candidate" 2"rep. candidate" 3"other candidate", modify
label val SenCand1Party_postr partycand
label val SenCand2Party_postr partycand
fre SenCand1Party_post SenCand1Party_postr
fre SenCand2Party_post SenCand2Party_postr

*republican vs dem incumbent
fre SenCand1Party_postr SenCand1Incumbent_postr
ta SenCand1Party_postr SenCand1Incumbent_postr
fre SenCand2Party_postr SenCand2Incumbent_postr
gen inc_rep_sen = 0 if (SenCand1Party_postr==1 & SenCand2Party_postr==2) & (SenCand1Incumbent_postr==1 & SenCand2Incumbent_postr==0)
replace inc_rep_sen = 1 if (SenCand1Party_postr==1 & SenCand2Party_postr==2) & (SenCand1Incumbent_postr==0 & SenCand2Incumbent_postr==1)
fre inc_rep_sen // 0= dem inc (rep not inc), 1= rep inc (dem not inc)
*including districts with no dem/rep incumbents
ta SenCand1Incumbent_postr SenCand2Incumbent_postr if (SenCand1Party_postr==1 & SenCand2Party_postr==2)
gen inc_all_sen = inc_rep_sen
recode inc_all_sen 0=-1
replace inc_all_sen = 0 if (SenCand1Party_postr==1 & SenCand2Party_postr==2) & (SenCand1Incumbent_postr==0 & SenCand2Incumbent_postr==0)
ta inc_all_sen // -1=dem inc, 0=inc not dem nor rep, 1=rep inc

*keep only states with elections, not open elections
ta state inc_rep, nol
*filter
/*gen count2=1 if inc_rep!=.
egen count_sum2 = sum(count2), by(state)
fre state if count_sum<10 & inc_rep!=.
ta state inc_rep if count_sum<=10, nol
ta state inc_rep if count_sum>10, nol
gen filter_sen2=1 if inc_rep!=.
replace filter_sen2=2 if count_sum2<10 & inc_rep!=.
replace filter_sen2=3 if (state==25 | state==28 | state==32 | state==47) & inc_rep==0
replace filter_sen2=3 if (state==10 | state==12 | state==34 | state==39 | state==53) & inc_rep==1
ta filter_sen2
ta state inc_rep if filter_sen2==1
clonevar filter_sen12 = filter_sen*/
ta SenCand1Name_post
bys state: ta SenCand1Name_post if inc_rep!=.
gen count=1 if inc_rep!=.
egen count_sum = sum(count), by(state SenCand1Name_post)
gen filter_sen=1 if inc_rep!=.
replace filter_sen=2 if count_sum<10 & inc_rep!=.
ta state inc_rep
ta state inc_rep if filter_sen==1
clonevar filter_sen12 = filter_sen

/*ta SenCand1Name_post SenCand2Name_post if state==24 & inc_rep!=.
ta SenCand1Name_post SenCand2Name_post if state==24 & filter_sen2==1
ta SenCand1Name_post SenCand2Name_post if state==24 & filter_sen==1 // the new filter works better!

ta SenCand1Name_post SenCand2Name_post if state==54 & inc_rep!=.
ta SenCand1Name_post SenCand2Name_post if state==54 & filter_sen2==1
ta SenCand1Name_post SenCand2Name_post if state==54 & filter_sen==1*/


****************
*contextual var HOUSE

*incumbent candidates
des HouseCand1Incumbent_post HouseCand2Incumbent_post
fre HouseCand1Incumbent_post HouseCand2Incumbent_post
encode(HouseCand1Incumbent_post), gen(HouseCand1Incumbent_postr)
encode(HouseCand2Incumbent_post), gen(HouseCand2Incumbent_postr)
fre HouseCand1Incumbent_postr HouseCand2Incumbent_postr
recode HouseCand1Incumbent_postr 1=0 2=1
recode HouseCand2Incumbent_postr 1=0 2=1
label val HouseCand1Incumbent_postr incumb
label val HouseCand2Incumbent_postr incumb
fre HouseCand1Incumbent_postr HouseCand2Incumbent_postr
ta HouseCand1Incumbent_postr HouseCand2Incumbent_postr

*party of candidate
des HouseCand1Party_post HouseCand2Party_post
fre HouseCand1Party_post HouseCand2Party_post
encode HouseCand1Party_post, gen(HouseCand1Party_postr)
encode HouseCand2Party_post, gen(HouseCand2Party_postr)
fre HouseCand1Party_postr HouseCand2Party_postr
ta HouseCand1Party_postr HouseCand2Party_postr
recode HouseCand1Party_postr 1=1 2=2
recode HouseCand2Party_postr 1=1 2=3 3=2
label val HouseCand1Party_postr partycand
label val HouseCand2Party_postr partycand
fre HouseCand1Party_post HouseCand1Party_postr
fre HouseCand2Party_post HouseCand2Party_postr // 2=rep

*republican incumbent vs. dem incumbent
*note: with this var we restrict analysis to seats with dem vs. rep AND with inc. vs. not inc.
fre HouseCand1Party_postr HouseCand1Incumbent_postr
fre HouseCand2Party_postr HouseCand2Incumbent_postr
ta HouseCand1Party_postr HouseCand2Party_postr // no cases when candidate 1 is republican and candidate 2 is democrat
gen inc_rep_hou = 0 if (HouseCand1Party_postr==1 & HouseCand2Party_postr==2) ///
& (HouseCand1Incumbent_postr==1 & HouseCand2Incumbent_postr==0)
replace inc_rep_hou = 1 if (HouseCand1Party_postr==1 & HouseCand2Party_postr==2) ///
& (HouseCand1Incumbent_postr==0 & HouseCand2Incumbent_postr==1)
fre inc_rep_hou
ta HouseCand1Incumbent_postr HouseCand2Incumbent_postr if inc_rep_hou!=.
*including districts with no dem/rep incumbents
ta HouseCand1Incumbent_postr HouseCand2Incumbent_postr if (HouseCand1Party_postr==1 & HouseCand2Party_postr==2) 
gen inc_all_hou = inc_rep_hou
recode inc_all_hou 0=-1
replace inc_all_hou = 0 if (HouseCand1Party_postr==1 & HouseCand2Party_postr==2) & (HouseCand1Incumbent_postr==0 & HouseCand2Incumbent_postr==0)
fre inc_all_hou // -1=dem inc, 0=inc not dem nor rep, 1=rep inc

*keep only states with elections, not open elections
ta state inc_rep_hou
*filter
gen count1=1 if inc_rep_hou!=.
egen count_sum1 = sum(count1), by(state HouseCand1Name_post)
gen filter_hou=1 if inc_rep_hou!=.
replace filter_hou=2 if count_sum1<10 & inc_rep_hou!=.
ta state inc_rep_hou if filter_hou==1
*double check
bys state: ta HouseCand1Name_post if filter_hou==1 // e.g. north dakota missing because no incumbent
fre state if HouseCand1Name_post=="Pam Gulleson" // seat without incumbent
fre state if HouseCand1Name_post=="Pam Gulleson" & filter_hou==1 // removed when filter applied
clonevar filter_hou12 = filter_hou


*******************
*combined variables > vote + incumbent status

*vote + incumbent status, all options - SENATE
ta vote_sen inc_all_sen
fre turnout vote_sen
fre inc_all_sen // -1=dem inc, 0=inc not dem nor rep, 1=rep inc
fre SenCand1Party_postr SenCand2Party_postr
gen votinc_sen_all = 0 if turnout<5 | vote_sen==5 | vote_sen==6 // did not vote
replace votinc_sen_all = 10 if vote_sen==1 & inc_all_sen==-1 & SenCand1Party_postr==1
replace votinc_sen_all = 11 if vote_sen==1 & inc_all_sen==0 & SenCand1Party_postr==1
replace votinc_sen_all = 12 if vote_sen==1 & inc_all_sen==1 & SenCand1Party_postr==1
replace votinc_sen_all = 20 if vote_sen==2 & inc_all_sen==-1 & SenCand2Party_postr==2
replace votinc_sen_all = 21 if vote_sen==2 & inc_all_sen==0 & SenCand2Party_postr==2
replace votinc_sen_all = 22 if vote_sen==2 & inc_all_sen==1 & SenCand2Party_postr==2
*label var votinc_sen_all "Voted for dem/rep by incumbent status - SENATE"
*label de votinc_sen_all 0"did not vote" 10"vote dem, dem inc"  11"vote dem, no inc"  12"vote dem, rep inc" ///
*20"vote rep, dem inc"  21"vote rep, no inc"  22"vote rep, rep inc" 
*label val votinc_sen_all votinc_sen_all
fre votinc_sen_all

*vote + incumbent status, all options - HOUSE
ta vote_hou inc_all_hou
fre inc_all_hou // -1=dem inc, 0=inc not dem nor rep, 1=rep inc
fre vote_hou
fre HouseCand1Party_postr HouseCand2Party_postr 
gen votinc_hou_all = 0 if turnout<5 | vote_hou==8 | vote_hou==9 // did not vote
replace votinc_hou_all = 10 if vote_hou==1 & inc_all_hou==-1 & HouseCand1Party_postr==1
replace votinc_hou_all = 11 if vote_hou==1 & inc_all_hou==0 & HouseCand1Party_postr==1 
replace votinc_hou_all = 12 if vote_hou==1 & inc_all_hou==1 & HouseCand1Party_postr==1
replace votinc_hou_all = 20 if vote_hou==2 & inc_all_hou==-1 & HouseCand2Party_postr==2
replace votinc_hou_all = 21 if vote_hou==2 & inc_all_hou==0 & HouseCand2Party_postr==2
replace votinc_hou_all = 22 if vote_hou==2 & inc_all_hou==1 & HouseCand2Party_postr==2
*label var votinc_hou_all "Voted for dem/rep by incumbent status - HOUSE"
*label val votinc_hou_all votinc_sen_all
fre votinc_hou_all

***
gen year = 2012

rename V101 caseid12
clonevar caseid = caseid12

ta tookpost


****
*create congressional districts
decode state, gen(stated)
drop if stated=="District of Columbia"
encode stated, gen(staten)
label var staten "States from 1 to 50 in alphabetical order"
fre staten // alphabetical order, from 0 to 50

*combine with district
rename cdid113_post cd2012str
encode cd2012str, gen(cd2012)
fre cd2012str // problem value 1 instead of 01, 2 instead of 02, etc.
fre cd2012str if staten==2

*turn 1 into 01, 2 into 02 etc.
gen cd2012_first = (substr(cd2012str,1,1))
gen cd2012_second = (substr(cd2012str,2,1))
encode cd2012_first, gen(cd2012_firsten)
encode cd2012_second, gen(cd2012_seconden)
fre cd2012_first if cd2012_seconden==.
gen value0 = 0
egen cd2012_first_correct=concat(value0 cd2012_first) if cd2012_firsten!=. & cd2012_seconden==.
fre cd2012_first cd2012_first_correct if cd2012_seconden==.

gen cd2012str_correct = cd2012_first_correct if cd2012_seconden==.
replace cd2012str_correct = cd2012str if cd2012_seconden!=.
label var cd2012str_correct "Post  Congressional district 2012 1=01"
fre cd2012str cd2012str_correct if staten==2
fre cd2012str cd2012str_correct if staten==9
fre cd2012str cd2012str_correct if staten==50

egen stcd=concat(staten cd2012str_correct)

*double check > all correct
fre stcd if staten==9 // florida
fre stcd if staten==9 & cd2012!=.
fre stcd if staten==32 // ny
fre stcd if staten==32 & cd2012!=.
fre stcd if staten==45 // vermont
fre stcd if staten==45 & cd2012!=.
fre stcd if staten==50 // wyoming
fre stcd if staten==50 & cd2012!=.

*
rename stcd stcd_string
gen stcd = real(stcd_string) // turn into numeric
label var stcd_string "State (1 to 50 alphabetical) + congr. district (01 to n) STRING"
label var stcd "State (1 to 50 alphabetical) + congr. district (01 to n)"

***
/*merge with contextual var

save "CCES_2012_recoded2.dta", replace
import excel "C:\Users\Davide Morisi\Dropbox\research_inprogress\incumbency_effect\Other files\hr4618.xls", sheet("2012") firstrow clear
sum year stcd inc pwin dv dvp fr po1 po2 redist dexp rexp dpres switchb switcha
rename dvp dvp_orig
rename po1 po1_orig
rename po2 po2_orig
gen dvp = real(dvp_orig) // transform dv var into numeric
gen po1 = real(po1_orig)
gen po2 = real(po2_orig)
sum year stcd inc pwin dv dvp fr po1 po2 redist dexp rexp dpres switchb switcha
list po1 po1_orig dvp dvp_orig in 100/120
list po1 po1_orig dvp dvp_orig in 50/70
drop year dvp_orig po1_orig po2_orig switchb switcha
save "C:\Users\Davide Morisi\Dropbox\research_inprogress\incumbency_effect\Replication do files\Data\house_data_2012.dta", replace
use "CCES_2012_recoded2.dta", clear
*/

*merge info about previous election
merge m:1 stcd using "C:\Users\Davide Morisi\Dropbox\research_inprogress\incumbency_effect\Replication do files\Data\house_data_2010.dta" // 11 not matched from using
drop pwin dv dvp fr redist dexp rexp dpres _merge
rename inc inc_previous
rename po1 po1_previous
rename po2 po2_previous

*post election
merge m:1 stcd using "C:\Users\Davide Morisi\Dropbox\research_inprogress\incumbency_effect\Replication do files\Data\house_data_2014.dta" // all matched
drop pwin dv dvp fr redist dexp rexp dpres _merge
rename inc inc_post
rename po1 po1_post
rename po2 po2_post

merge m:1 stcd using "C:\Users\Davide Morisi\Dropbox\research_inprogress\incumbency_effect\Replication do files\Data\house_data_2012.dta" // all matched from using
drop _merge

***
*voter registration status post
fre votereg_post
recode votereg_post (2 3=0 "No/DK") (1=1 "Yes"), gen(register_post)
label var register_post "Registered to vote? (post)"
fre register_post

***
*job approval
*Please indicate whether you approve or disapprove of the job that each of the following are doing.
* 1 Strongly approve
* 2  Approve
* 3 Disapprove
* 4 Strongly disapprove
* 5 never heard

*House representative
fre CC315a 
rename CC315a approval_house

*senate
*2 senators for each state. 
fre CC315b CC315c 
rename CC315b approval_sen1
rename CC315c approval_sen2

***
*keep only selected variables
keep gender birthyr educ racer religpewr state state12 weight1 economy ///
pid3 pid7 pid3post pid5post ///
ideo5 ideo7 ///
turnout turnout12 vote_sen votesen12 vote_hou votehou12 ///
SenCand1Incumbent_postr SenCand2Incumbent_postr SenCand1Party_postr SenCand2Party_postr ///
inc_rep_sen inc_all_sen filter_sen filter_sen12 ///
HouseCand1Incumbent_postr HouseCand2Incumbent_postr HouseCand1Party_postr HouseCand2Party_postr ///
inc_rep_hou inc_all_hou filter_hou filter_hou12 ///
votinc_sen_all votinc_hou_all ///
year caseid caseid12 tookpost ///
staten ///
cd2012 cd2012str cd2012str_correct stcd_string stcd ///
inc pwin dv dvp fr po1 po2 redist dexp rexp dpres ///
register_post approval_house approval_sen1 approval_sen2 ///
inc_previous po1_previous po2_previous ///
inc_post po1_post po2_post

*save recoded file
save "CCES_2012_recoded2.dta", replace






